<template>
  <div>
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      v-dialogDrag
      :visible.sync="visible">
      <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"
               :disabled="method==='view'"
               label-width="120px">
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="商家ID" prop="merchant.id"
                          :rules="[
                  {required: true, message:'商家ID不能为空', trigger:'blur'}
                 ]">
              <GridSelect
                title="选择商家ID"
                labelName='merchantName'
                labelValue='id'
                :value="inputForm.merchant.id"
                :limit="1"
                @getValue='(value) => {inputForm.merchant.id=value}'
                :columns="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'merchantName',
              label: '商家名称'
            },
            {
              prop: 'storeAddress',
              label: '店铺地址'
            }
            ]"
                :searchs="[
            {
              prop: 'merchantName',
              label: '商家名称'
            }
            ]"
                dataListUrl="/vmerchantinformation/vMerchantInformation/list"
                entityBeanName="vMerchantInformation"
                queryEntityUrl="/vmerchantinformation/vMerchantInformation/queryById">
              </GridSelect>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="礼品ID" prop="gift.id"
                          :rules="[
                  {required: true, message:'礼品ID不能为空', trigger:'blur'}
                 ]">
              <GridSelect
                title="选择礼品ID"
                labelName='giftName'
                labelValue='id'
                :value="inputForm.gift.id"
                :limit="1"
                @getValue='(value) => {inputForm.gift.id=value}'
                :columns="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'giftName',
              label: '礼品名称'
            },
            {
              prop: 'stock',
              label: '库存'
            }
            ]"
                :searchs="[
            {
              prop: 'giftName',
              label: '礼品名称'
            }
            ]"
                dataListUrl="/vgiftinformation/vGiftInformation/list"
                entityBeanName="vGiftInformation"
                queryEntityUrl="/vgiftinformation/vGiftInformation/queryById">
              </GridSelect>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item :label="method == 'add' ? '分配库存' : '调整库存'" prop="stock"
                          :rules="[
                  
                 ]">
              <el-input type="Number" v-model="inputForm.stock" placeholder="请填写库存"></el-input>
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="库存预警值" prop="inventoryAlertValue"
                          :rules="[
                  {validator: validator.isIntGtZero, trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.inventoryAlertValue" placeholder="请填写库存"></el-input>
            </el-form-item>
          </el-col>

          <!-- <el-col :span="12" v-if="method == 'edit'">
              <el-form-item label="是否调整库存" prop="adjustInventory"
                  :rules="[
                    {validator: validator.isIntGtZero, trigger:'blur'}
                   ]">
                <el-radio-group v-model="inputForm.adjustInventory">
                  <el-radio label="1">是</el-radio>
                  <el-radio label="0">否</el-radio>
                </el-radio-group>
             </el-form-item>
          </el-col> -->
          <!-- <el-col :span="12">
              <el-form-item label="备注信息" prop="remarks"
                  :rules="[
                   ]">
            <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
             </el-form-item>
          </el-col> -->
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
import GridSelect from '@/components/gridSelect'

export default {
  data() {
    return {
      title: '',
      method: '',
      visible: false,
      loading: false,
      inputForm: {
        id: '',
        merchant: {
          id: ''
        },
        gift: {
          id: ''
        },
        stock: '0',
        inventoryAlertValue: '',
        remarks: '',
        adjustInventory: "0",
        deleteFlag: '0'
      }
    }
  },
  components: {
    GridSelect
  },
  methods: {
    init(method, id) {
      this.method = method
      this.inputForm.id = id
      if (method === 'add') {
        this.title = `新建商家礼品分配信息列表`
      } else if (method === 'edit') {
        this.title = '修改商家礼品分配信息列表'
      } else if (method === 'view') {
        this.title = '查看商家礼品分配信息列表'
      }
      this.visible = true
      this.loading = false
      this.$nextTick(() => {
        this.$refs.inputForm.resetFields()
        if (method === 'edit' || method === 'view') { // 修改或者查看
          this.loading = true
          this.$http({
            url: `/vmerchantgiftdistribution/vMerchantGiftDistribution/queryById?id=${this.inputForm.id}`,
            method: 'get'
          }).then(({data}) => {

            this.inputForm = this.recover(this.inputForm, data.vMerchantGiftDistribution)
            this.inputForm.adjustInventory = "0"
            this.inputForm.stock = 0
            this.loading = false
          })
        }
      })
    },
    // 表单提交
    doSubmit() {
      this.$refs['inputForm'].validate((valid) => {
        this.inputForm.adjustInventory = "1"
        if (valid) {
          this.loading = true
          this.$http({
            url: `/vmerchantgiftdistribution/vMerchantGiftDistribution/save`,
            method: 'post',
            data: this.inputForm
          }).then(({data}) => {
            this.loading = false
            if (data && data.success) {
              this.visible = false
              this.$message.success(data.msg)
              this.$emit('refreshDataList')
            }
          })
        }
      })
    }
  }
}
</script>
